<template>
  <div class="login-container">
    <!-- 头部区域 -->
    <div class="logo">
      <img src="../../assets/images/logo.png" alt="" />
    </div>
    <!-- 登录和注册区域 -->
    <div class="form-box">
      <div class="logo-title">
        <img src="../../assets/images/login_title.png" alt="" />
      </div>
      <el-form
        :model="loginForm"
        :rules="rulesForm"
        ref="loginForm"
        class="demo-loginForm"
      >
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-button type="primary" class="login-btn" @click="btnSubmit"
          >登录</el-button
        >
      </el-form>
      <el-link @click="$router.push('/reg')" target="_blank"
        >去注册账号</el-link
      >
    </div>
  </div>
</template>
<script>
import { loginAxios } from '@/api/user'
export default {
  name: '',
  data () {
    return {
      loginForm: {
        username: 'yy1224',
        password: '123456'
      },
      rulesForm: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '请输入3-10位用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '请输入3-16位密码', trigger: 'blur' }
        ]
      }
    }
  },
  created () {},
  computed: {},
  methods: {
    /*
      登录按钮点击：
      + 预校验
      + 发送请求
      + 判断请求是否成功
        + 提示登录成功
        + token做本地缓存
        + 跳转到首页
       */
    btnSubmit () {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          // 校验成功
          // 校验成功 后发送axios请求
          const res = await loginAxios(this.loginForm)
          console.log(res, 666)
          if (res.data.code === 0) {
            this.$message({
              message: '恭喜你，校验成功',
              type: 'success'
            })
          }
          /* 存储token  因为其他页面访问需要有token */
          localStorage.setItem('token', res.data.token)
          // 路由携带参数参进行跳转
          /* 跳转到首页 */
          this.$router.push('/')
        } else {
          //  校验不成功
          this.$message({
            message: '校验不成功',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  // background-color: pink;
  background: url('../../assets/images/login_bg.png') no-repeat center / cover;
  .logo {
    width: 1200px;
    margin: 0 auto;
  }
  .form-box {
    width: 400px;
    height: 270px;
    background-color: #fff;

    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 20px 30px;
    box-sizing: border-box;
    .logo-title {
      text-align: center;
      margin-bottom: 15px;
    }
    .el-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
